using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace Easislides
{
	public class PopupWindowHelperMessageFilter : IMessageFilter
	{
		private const int WM_LBUTTONDOWN = 513;
		private const int WM_RBUTTONDOWN = 516;
		private const int WM_MBUTTONDOWN = 519;
		private const int WM_NCLBUTTONDOWN = 161;
		private const int WM_NCRBUTTONDOWN = 164;
		private const int WM_NCMBUTTONDOWN = 167;
		private Form popup = null;
		private PopupWindowHelper owner = null;
		public event PopupCancelEventHandler PopupCancel
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			add
			{
                //this.PopupCancel = (PopupCancelEventHandler)Delegate.Combine(this.PopupCancel, value);
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			remove
			{
                //this.PopupCancel = (PopupCancelEventHandler)Delegate.Remove(this.PopupCancel, value);
			}
		}
		public Form Popup
		{
			get
			{
				return this.popup;
			}
			set
			{
				this.popup = value;
			}
		}
		public PopupWindowHelperMessageFilter(PopupWindowHelper owner)
		{
			this.owner = owner;
		}
		public bool PreFilterMessage(ref Message m)
		{
			if (this.popup != null)
			{
				int msg = m.Msg;
				if (msg <= 167)
				{
					if (msg != 161 && msg != 164 && msg != 167)
					{
						goto IL_5B;
					}
				}
				else
				{
					if (msg != 513 && msg != 516 && msg != 519)
					{
						goto IL_5B;
					}
				}
				this.OnMouseDown();
				IL_5B:;
			}
			return false;
		}
		private void OnMouseDown()
		{
			Point position = Cursor.Position;
			if (!this.popup.Bounds.Contains(position))
			{
				this.OnCancelPopup(new PopupCancelEventArgs(this.popup, position));
			}
		}
		protected virtual void OnCancelPopup(PopupCancelEventArgs e)
		{
            //if (this.PopupCancel != null)
            //{
            //    this.PopupCancel(this, e);
            //}
            //if (!e.Cancel)
            //{
            //    this.owner.ClosePopup();
            //    this.popup = null;
            //}
		}
	}
}
